package com.vive.tree;

import org.apache.wicket.extensions.markup.html.tree.Tree;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.Link;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class TreePage extends WebPage {
    public TreePage(){
        super();

        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
        rootNode.setUserObject(this);

        for (int i = 0; i < 5; i++) {
            DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode("parent-" + i);
            ;
            for (int p = 0; p < 5; p++) {
                DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("child=" + p);

                parentNode.add(childNode);
            }
            rootNode.add(parentNode);
        }

        DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
        final Tree tree = new Tree("tree",treeModel){
            protected String getNodeLabel(DefaultMutableTreeNode node){
                if (node.getUserObject() == TreePage.this) {
                    return "root";
                }else {
                    return node.toString();
                }
            }
        };

        add(tree);

        add(new Link("expandAll") {

            @Override
            public void onClick() {
                tree.getTreeState().expandAll();
            }
        });


        add(new Link("collapseAll") {
            @Override
            public void onClick() {
                tree.getTreeState().collapseAll();
            }
        });

        }
    }

